<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Happy ERP</title>
    <%= stylesheet_link_tag "../javascripts/ext/resources/css/ext-all.css" %>

    <%= stylesheet_link_tag "images" %>
    <%= stylesheet_link_tag "ext-patch" %>

    <%= javascript_include_tag "ext/adapter/ext/ext-base.js" %>
    <%= javascript_include_tag "ext/ext-all.js" %>
    <%= javascript_include_tag "ext/src/locale/ext-lang-zh_CN.js" %>
    
    <script type="text/javascript">
      Ext.onReady(function(){          
        Ext.QuickTips.init();
        var userName = new Ext.form.TextField({
          fieldLabel: '登录名称',
          name: 'user_name',
          allowBlank: false,
          blankText: "登录名不能为空",
          anchor: '90%',
          listeners: {
            scope: this,
            keypress: function(field, e) {
              if (e.getKey() == 13) {
                loginPwd.focus(true);
              }
            }
          }
        })

        var password = new Ext.form.TextField({
          fieldLabel: '登录密码',
          name: 'pwd',
          inputType: 'password',
          allowBlank: false,
          blankText: "登录密码不能为空",
          anchor: '90%',
          listeners: {
            specialkey:function(field,e){
              if (e.getKey()==Ext.EventObject.ENTER){
                loginButton.handler();
              }
            } 
          }
        })
        
        var loginButton = new Ext.Button({
          text: '    登  录    ',
          handler: function(){
            loginButton.setDisabled(true);
            userName.setReadOnly(true);
            password.setReadOnly(true);
            if (loginForm.form.isValid()) {
              loginForm.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    window.document.location.href = '/';
                    //window.location.replace('/');
                  } else {
                    Ext.MessageBox.show({
                      title: '消息',
                      msg: resp.result.info,
                      buttons: Ext.MessageBox.OK,
                      icon: Ext.MessageBox.WARNING
                    });
                    loginButton.enable();
                    userName.setReadOnly(false);
                    password.setReadOnly(false);
                  }
                },
                failure: function(result, request){
                  loginButton.enable();
                  Ext.MessageBox.show({
                    title: '消息',
                    msg: "登录过程中和服务器的通信失败!",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }
              });
            }
            else {
              loginButton.enable();              
              Ext.MessageBox.show({
                title: '消息',
                msg: "请输入必输选项",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
              });
            }
          }
        })
            
        var loginForm = new Ext.FormPanel({
          labelWidth: 65, // label settings here cascade unless overridden
          url:'/index/validate_user',
          frame:true,
          labelAlign: 'right',
          method: 'GET',
          //bodyStyle:'padding:5px 5px 0',
          width: 440,
          //defaults: {width: 180},
          defaultType: 'textfield',
          items: [{
              id: 'formImg',
              columnWidth: 1,
              bodyStyle: Ext.isIE ? 'padding:15px 0 5px 15px;' : 'padding:10px 15px;',
              border: false,
              xtype: 'panel',
              html: '<img src = "/images/welcome<%=@imageNumber%>.jpg" width="400" height="266"/>'
            },{
              id: 'formTable',
              columnWidth: 1,
              xtype: 'panel',
              labelWidth: 70,
              buttonAlign: 'center',
              layout: 'column',
              bodyStyle: Ext.isIE ? 'padding:15px 0 5px 15px;' : 'padding:10px 15px;',
              border: false,
              items: [{
                  columnWidth:.5,
                  layout:'form',
                  items:[userName]                //这两个一人一半
                }, {
                  columnWidth:.5,
                  layout:'form',
                  items:[password]                //这两个一人一半
                }
              ]},
            {
              fieldLabel: '公司编号',
              name: 'company_id',
              value:'1',
              hidden:true,
              allowBlank:false
            }
          ]
        });

        var loginWin = new Ext.Window({
          buttonAlign: 'center',
          closable: false,
          draggable: true,
          height: 410,
          region: 'center',
          //plain: false,
          resizable: false,
          items: [loginForm],
          title: '系统登录',
          width: 450,
          buttons: [loginButton]
        });
        loginWin.show();
      }
    )
    </script>
  </head>
  <body>
  </body>
</html>
